package cn.caidabao.gb.component.sip.server.response.recieve;

import cn.caidabao.gb.bean.OGSipCallIdCallBack;
import cn.caidabao.gb.component.sip.server.storage.CallBackStorage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sip.ResponseEvent;
import javax.sip.header.CallIdHeader;
import javax.sip.message.Response;

@Slf4j
@Component
public class InfoMessageResponseHandler implements ResponseHandler{
    @Resource
    private CallBackStorage callBackStorage;
    @Override
    public void handleResponse(ResponseEvent responseEvent) {
        Response response = responseEvent.getResponse();
        CallIdHeader callIdHeader = (CallIdHeader) response.getHeader(CallIdHeader.NAME);
        String callId = callIdHeader.getCallId();
        OGSipCallIdCallBack callBack = callBackStorage.getAndDeleteCallBack(callId);
        if (callBack==null){
            log.info("收到未知的info response：{}",callId);
        }else {
            byte[] contentByte = (byte[]) response.getContent();
            if (contentByte==null){
                callBack.setResult("empty response");
            }else {
                String content = new String(contentByte);
                callBack.setResult(content);
            }

        }

    }
}
